﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Infrastructure;
using MSIACCSL2.Web.WarehouseServices;

namespace MSIACCSL2.Web.ReportPage
{
    public partial class PS : System.Web.UI.Page
    {
        private DataAccess<Object> DA;
        private ReportDocument _presales;
        private ReportDocument _presalesSummary;
        private string Tipe;

        public PS()
        {
            DA = new DataAccess<object>();
            _presales = new ReportDocument();
            _presalesSummary = new ReportDocument();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
            if (IsPostBack)
                LoadReport(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
        }
      
    
        protected void Button1_Click(object sender, EventArgs e)
        {
          LoadReport(Request.QueryString["no"].ToString(), ViewState["tipe"].ToString());
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
        }


        public void LoadReport(string noPS, string tipe)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" Select a.*, ");
            sb.Append(" i.SerialNumber,i2.ItemName,s.SubGroupName,b.Panjang,b.Lebar,b.Tinggi,b.QtyUnit as Qty,i.NoBundle,i.SatuanPack,i.SatuanUnit ");
            sb.Append(" from PreSalesOrder a   ");
            sb.Append(" INNER JOIN ");
            sb.Append(" PreSalesOrderDetail b on a.PSNo = b.PSNo  ");
            sb.Append(" LEFT JOIN inventory i ON b.SerialNumber = i.SerialNumber ");
            sb.Append(" Left Join item i2 on i2.ItemId = i.ItemId ");
            sb.Append(" Left Join Subgroup s on s.SubgroupId = i2.SubgroupId ");
            sb.Append(" Where a.PSNo='" + noPS + "'  Order By s.SubgroupId,i.ItemId ");

            if (RadioButtonList2.SelectedValue == "Detail")
            {
                string reportPath = Server.MapPath("~/Report/PreSalesOrder.rpt");
                _presales.Load(reportPath);
                var Source = DA.GetDataListDataSet(sb.ToString());
                _presales.SetDataSource(Source.Tables[0]);
                var param = new ParameterDiscreteValue();
                param.Value = tipe;
                var paramValue = new ParameterValues();
                paramValue.Add(param);
                _presales.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
                CrystalReportViewer1.ReportSource = _presales;
            }
            else
            {
                string reportPath = Server.MapPath("~/Report/PreSalesOrderSummary.rpt");
                _presalesSummary.Load(reportPath);
                var Source = DA.GetDataListDataSet(sb.ToString());
                _presalesSummary.SetDataSource(Source.Tables[0]);
                var param = new ParameterDiscreteValue();
                param.Value = tipe;
                var paramValue = new ParameterValues();
                paramValue.Add(param);
                _presalesSummary.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
                CrystalReportViewer1.ReportSource = _presalesSummary;
            }


            //crystalReportViewer1.RefreshReport();
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.Unload += Page_Unload;
        }

        void Page_Unload(object sender, EventArgs e)
        {
            _presales.Clone();
            _presales.Dispose();

            _presalesSummary.Clone();
            _presalesSummary.Dispose();
        }
    }
}